package com.wfs.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Set;

public class WebServiceUtil {
	
	private static final String URL_ENCODING = "UTF-8";
	
	/**
	 * Parses parameters from Map collection to URL encoded HTTP parameters
	 * @param param Map<String,String> collection to be parsed
	 * @return URL encoded HTTP parameter string
	 */
	public static String parseParameters(Map<String,String> param){
		
		StringBuilder sb = new StringBuilder();
		Set<String> keySet = param.keySet();

		int count = 0;
		for(String k : keySet){
			String v = param.get(k);
			try {
				sb.append(URLEncoder.encode(k,URL_ENCODING));
				sb.append("=");
				sb.append(URLEncoder.encode(v,URL_ENCODING));
				if(count < param.size()-1)
					sb.append("&");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			count++;
		}
		return sb.toString();
	}

}
